package com.yrihr.wisdom.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrihr.wisdom.dto.NewsDto;
import com.yrihr.wisdom.entity.News;
import com.yrihr.wisdom.service.NewsSercice;
import com.yrihr.wisdom.vo.NewsTypeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author by Hasee
 * @Classname IndexController
 * @Description  信息主页controller
 * @Date 2020/11/10 10:09
 */
@Controller
@RequiredArgsConstructor
public class IndexController {
    private final NewsSercice newsSercice;

    @RequestMapping("/index")
    public String index(Model model, Page<?> page,HttpServletRequest request) {
        NewsDto news = new NewsDto();
        String type = request.getParameter("type");
        if (StringUtils.isNotBlank(type)) {
            news.setType(Integer.parseInt(type));
        }
        String startTime = request.getParameter("startTime");
        if (StringUtils.isNotBlank(startTime)) {
            news.setStartTime(startTime);
        }
        String endTime = request.getParameter("endTime");
        if (StringUtils.isNotBlank(endTime)) {
            news.setEndTime(endTime);
        }
        String newsTitle = request.getParameter("newsTitle");
        if (StringUtils.isNotBlank(newsTitle)) {
            news.setNewsTitle(newsTitle);
        }
        Page<News> newsPageList = (Page<News>) newsSercice.getNewsPageList(page);
        List<NewsTypeVo> newsAllType = newsSercice.getNewsAllType(news);
        //查询新闻总条数
        Integer allNewsCount = newsSercice.getAllNewsCount(news);
        if (allNewsCount != null) {
            model.addAttribute("count", allNewsCount);
        }
        model.addAttribute("type", newsAllType);
        model.addAttribute("page", newsPageList);
        return "success";
    }

    @RequestMapping("/selectType")
    public String selectType(Model model, Page<?> page, HttpServletRequest request) {
        NewsDto news = new NewsDto();
        String type = request.getParameter("type");
        if (StringUtils.isNotBlank(type)) {
            news.setType(Integer.parseInt(type));
        }
        String startTime = request.getParameter("startTime");
        if (StringUtils.isNotBlank(startTime)) {
            news.setStartTime(startTime);
        }
        String endTime = request.getParameter("endTime");
        if (StringUtils.isNotBlank(endTime)) {
            news.setEndTime(endTime);
        }
        String newsTitle = request.getParameter("newsTitle");
        if (StringUtils.isNotBlank(newsTitle)) {
            news.setNewsTitle(newsTitle);
        }
        Page<News> newsIPage = (Page<News>) newsSercice.selectPageNews(page, news);
        List<NewsTypeVo> newsAllType = newsSercice.getNewsAllType(news);
        //查询新闻总条数
        Integer allNewsCount = newsSercice.getAllNewsCount(news);
        if (allNewsCount != null) {
            model.addAttribute("count", allNewsCount);
        }
        model.addAttribute("type", newsAllType);
        model.addAttribute("page", newsIPage);
        return "success::first";
    }
}
